using System.Collections.Generic;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Collision2D;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class TotemComponent : TComponent
    {
        private Totem totem;

        private SkillEffective_Totem_ConfigData _config;
        private SkillEffectiveContext _context;

        // 注意 蓄力冲锋图腾不允许被图腾调用
        public void Initialize(SkillEffective_Totem_ConfigData totemConfig, SkillEffectiveContext context)
        {
            this._config = totemConfig;
            this._context = ClassPool<SkillEffectiveContext>.Get();
            // 在这里克隆记录一下
            this._context.skill = context.skill;
            this._context.part = context.part;
            this._context.param = context.param;
        }

        public bool TryActive(SkillEffective_Totem_ConfigData.EActiveType activeType, TotemResponse totemResp, out SkillEffectiveResponse resp)
        {
            resp = ClassPool.Get<SkillEffectiveResponse>();
            resp.effectiveType = (int)ESkillEffectiveType.Totem;
            resp.pod = totemResp;

            if (this._config.activeType != activeType)
            {
                // 若是Now的时候还包含创建图腾的功能
                if (activeType != SkillEffective_Totem_ConfigData.EActiveType.Now)
                {
                    ClassPool.Put(resp);
                    resp = null;
                }
                return false;
            }
            List<Unit> rAllUnits = ListPool<Unit>.Get();

            var rProxy = ColliderProxy.Fetch();
            rProxy.Init(CollisionUtil.GetCirclePrefab(new LFloat(true, this._config.range)), this.totem.pos);
            rProxy.IsStatic = false;
            rProxy.LayerType = 0;
            this.totem.battle.playCtrl.collisionCtrl.CheckCollision(rProxy, this.OnHit, ref rAllUnits, this.TargetCondition);
            ColliderProxy.Free(rProxy);

            for (int i = 0; i < rAllUnits.Count; i++)
            {
                var rTargetUnit = rAllUnits[i];
                totemResp.packageResp.Add(SkillEffective_Package.PackageOnExecute(this._config.actions, rTargetUnit, ref this._context, 0) as SkillEffectivePackageResponse);
            }
            ListPool<Unit>.Put(rAllUnits);
            return true;
        }

        protected override void OnDispose()
        {
        }

        public override void OnCreate(TEntity entity)
        {
            this.totem = entity as Totem;
        }

        private void OnHit(List<CollisionComponent> rResult, List<Unit> rTargets)
        {
            for (int i = 0; i < rResult.Count; i++)
            {
                var rUnit = rResult[i].unit;
                if (rUnit != null)
                {
                    rTargets.Add(rUnit);
                }
            }
        }

        private bool TargetCondition(CollisionComponent rTarget)
        {
            var rUnit = rTarget.unit;
            if (rUnit == null ||
                rUnit.IsDead(true) ||
                rUnit.IsTotem())
            {
                return false;
            }

            var nTargetTeam = rUnit.teamIndex;
            var rRelation = BattleUtil.CheckRelation(this.totem, rUnit);
            if ((this._config.targetRelation & rRelation) != 0)
            {
                return true;
            }

            return false;
        }
    }
}